package com.jsoft.showcontacts;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class MainActivity extends FragmentActivity implements
		LoaderManager.LoaderCallbacks<Cursor> {

	ListView list;
	SimpleCursorAdapter adapter;

	static final VersionDependency pre_11 = new VersionDependency(new String[] {
			ContactsContract.Contacts.DISPLAY_NAME,
			ContactsContract.Contacts.PHOTO_ID }, new String[] {
			ContactsContract.Contacts._ID,
			ContactsContract.Contacts.DISPLAY_NAME,
			ContactsContract.Contacts.PHOTO_ID },
			PhotoViewBinder2_3.class
	);

	static final VersionDependency post_11 = new VersionDependency(
			new String[] { ContactsContract.Contacts.DISPLAY_NAME,
					ContactsContract.Contacts.PHOTO_THUMBNAIL_URI },
			new String[] { ContactsContract.Contacts._ID,
					ContactsContract.Contacts.DISPLAY_NAME,
					ContactsContract.Contacts.PHOTO_THUMBNAIL_URI },
			null);

	static VersionDependency versionDependency = null;
	static {
		if (android.os.Build.VERSION.SDK_INT >= 11)
			versionDependency = post_11;
		else
			versionDependency = pre_11;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		list = (ListView) findViewById(R.id.list);
		adapter = new SimpleCursorAdapter(this, R.layout.top_list_entry, null,
				versionDependency.adapterFrom,
				new int[] { R.id.name,	R.id.photo },
				0);
		
		if (versionDependency.viewBinderClass != null)
		{
			ViewBinder viewBinder =  adapter.getViewBinder();
			SimpleCursorAdapter_DelegateViewBinder newViewBinder;
			try {
				newViewBinder = versionDependency.viewBinderClass.newInstance();
				newViewBinder.setViewBinderDelegate(viewBinder);
				newViewBinder.setContentResolver(getContentResolver());
				adapter.setViewBinder(newViewBinder);
			} catch (Exception ex) {
				System.err.println(ex);
			}
		}

		list.setAdapter(adapter);

		ViewTreeObserver observer = list.getViewTreeObserver();
		
		
		observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				
				System.err.println("global layout");
				
				int count = list.getChildCount();
				int maxHeight = 0;
				int i;
				
				for (i = 0; i < count; i++) {
					View child = list.getChildAt(i);
					if (!(child instanceof LinearLayout))
						continue;
					
					LinearLayout ll = (LinearLayout)child;
					if (ll.getChildCount() != 2)
						continue;
					
					View llChild = ll.getChildAt(1);
					if (!(llChild instanceof ImageView))
						continue;
					ImageView iv = (ImageView) llChild;
					
					int childHeight = iv.getHeight();
					if (maxHeight < childHeight)
						maxHeight = childHeight;
					
				}
				
				for (i = 0; i < count; i++) {
					View child = list.getChildAt(i);
					if (!(child instanceof LinearLayout))
						continue;
					
					LinearLayout ll = (LinearLayout)child;
					if (ll.getChildCount() != 2)
						continue;
					
					View llChild = ll.getChildAt(1);
					if (!(llChild instanceof ImageView))
						continue;
					ImageView iv = (ImageView) llChild;
					
					int childHeight = iv.getHeight();
					if (childHeight < maxHeight)
						iv.setMinimumHeight(maxHeight);
				}
				
			}
		});
		
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent showContactIntent = new Intent(MainActivity.this, ShowContactActivity.class);
				showContactIntent.putExtra("contactId", id);
				startActivity(showContactIntent);
				
			}
		});
				
				
				
		getSupportLoaderManager().initLoader(0, savedInstanceState, this);
		getSupportLoaderManager().restartLoader(0, savedInstanceState, this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		//Uri baseUri = ContactsContract.Contacts.CONTENT_URI;
		Uri baseUri = ContactsContract.Contacts.CONTENT_STREQUENT_URI;
		
		return new CursorLoader(this, baseUri, versionDependency.cursorProjection, null, null,
				ContactsContract.Contacts.DISPLAY_NAME
						+ " COLLATE LOCALIZED ASC");
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		adapter.swapCursor(cursor);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		adapter.swapCursor(null);

	}

	static class VersionDependency {
		public VersionDependency(String[] adapterFrom, String[] cursorProjection,
				Class<? extends SimpleCursorAdapter_DelegateViewBinder> viewBinderClass) {
			this.adapterFrom = adapterFrom;
			this.cursorProjection = cursorProjection;
			this.viewBinderClass = viewBinderClass;
		}

		String adapterFrom[];
		String cursorProjection[];
		Class<? extends SimpleCursorAdapter_DelegateViewBinder> viewBinderClass;
	}

}
